//
//  ShareViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/29.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareView.h"
#import "AllHeader.h"
#import "WXApi.h"

#import <UIImageView+WebCache.h>

@interface ShareViewController ()


// 模糊视图
@property (nonatomic, strong) UIVisualEffectView *visualeffectview;

// 分享按钮
@property (nonatomic, strong) ShareView *shareView;


@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    
//    [self createVisualEffectView];
    //  添加毛玻璃效果
    [UIView viewOfBlurEffect:self.view withAlpha:1.0];
    [self createShareView];
}

// 毛玻璃效果
- (void)createVisualEffectView{
    UIVisualEffect *lightBlurEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)];
    _visualeffectview = [[UIVisualEffectView alloc] initWithEffect:lightBlurEffect];
    _visualeffectview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [self.view addSubview:_visualeffectview];
}

// 创建微信分享选项卡
- (void)createShareView{

    _shareView = [[ShareView alloc] initWithFrame:CGRectMake(25, SCREEN_HEIGHT /2.0 - 136, SCREEN_WIDTH - 50, 136)];
    _shareView.backgroundColor = [UIColor whiteColor];
//    [UIView viewShadowColor:_shareView];
    [UIView viewWideForShadowColor:_shareView];
    _shareView.layer.cornerRadius = 10;
    [self.view addSubview:_shareView];

    __weak ShareViewController *wShareVC = self;
    _shareView.shareblock = ^void(NSInteger index){
        if (index == 0) {
            [wShareVC shareToMomentsOfWX];
        } else {
            [wShareVC shareToFriendOfWX];
        }
    };
}

// 分享到好友会话
- (void)shareToFriendOfWX{
    //  多媒体消息结构体
     WXMediaMessage *message = [WXMediaMessage message];
    message.title = _shareString;
    message.description = _shareString;

    //  多媒体消息中包含的图片数据对象
     WXImageObject *ext = [WXImageObject object];

    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_imageURL] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
         ext.imageData = UIImagePNGRepresentation(image);
    }];
    message.mediaObject = ext;
    
    //  第三方程序发送消息至微信终端程序的消息结构体
    SendMessageToWXReq *sendMsg = [[SendMessageToWXReq alloc] init];
    sendMsg.bText = NO;
    sendMsg.scene =  WXSceneTimeline; // 朋友圈
    sendMsg.message = message;
    
    [WXApi sendReq:sendMsg];

}

// 分享到朋友圈
- (void)shareToMomentsOfWX{
    SendMessageToWXReq *sendMsg = [[SendMessageToWXReq alloc] init];
    sendMsg.text =  @"朋友圈";
    sendMsg.bText = YES;
    sendMsg.scene = WXSceneSession; // 好友列表
    [WXApi sendReq:sendMsg];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if ( point.x < _shareView.left || point.x > _shareView.right || point.y < _shareView.top || point.y > _shareView.bottom) {
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
            self.view.frame = CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
        } completion:^(BOOL finished) {
            
        }];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
